// page_two/yilqix/yilqix.js
let app = getApp()
let yuming = app.globalData.yuming
var shuzong = []
var shu = []
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    yuming: yuming,
    heights: "",
    title: '设施设备',
    buttonClicked: false,
    huan: ['设备信息', '添加设施设备'],
    currtab: 0,
    page: 1,
    ylqxList: [],
    // ylqxList: [{
    //   createtime: '2022-04-21',
    //   cpname: '器械',
    //   tyname: '器械',
    //   gg: '规格',
    //   xh: '型号',
    //   sccs: '生产厂商',
    //   bzwh: '报准文号',
    //   ph: '批号',
    //   scdate: '生产日期',
    //   yxqz: '有效期至',
    //   num: '数量',
    //   lz: `${yuming}static/applet/images/xiaofei/images/linshi/wem.jpg`,
    //   jybg: `${yuming}static/applet/images/xiaofei/images/linshi/wyn.jpg`,
    //   hgz: `${yuming}static/applet/images/xiaofei/images/linshi/yxc.jpg`
    // }],
    cpname: '',
    tyname: '',
    gg: '',
    xh: '',
    sccs: '',
    bzwh: '',
    ph: '',
    scdate: '请选择生产日期',
    yxqz: '请选择有效期截止时间',
    num: 1,
    lz: '',
    wulz: '',
    jybg: '',
    wujybg: '',
    hgz: '',
    wuhgz: '',
    wuphoto: '',
    gongwu: false,
  },
  qies(e) {
    shuzong = []
    shu = []
    var index = e.currentTarget.dataset.currtab
    this.setData({
      currtab: index,
      page: 1,
      cpname: '',
      tyname: '',
      gg: '',
      xh: '',
      sccs: '',
      bzwh: '',
      ph: '',
      scdate: '请选择生产日期',
      yxqz: '请选择有效期截止时间',
      num: 1,
      lz: '',
      wulz: '',
      jybg: '',
      wujybg: '',
      hgz: '',
      wuhgz: '',
      gongwu: false,
    })
    if (this.data.currtab == 0) {
      this.getylqxList();
    }
  },
  // 查看更多
  lookmore() {
    shuzong = this.data.ylqxList
    this.setData({
      page: (this.data.page) + 1
    })
    this.getylqxList();
  },
  // 医疗器械列表接口
  getylqxList() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    let that = this
    this.myRequest(yuming + 'Yiyao/yiyaoQixieList', {
      shopId: wx.getStorageSync('shopId'),
      page: that.data.page
    }, "POST").then(res => {
      if (res.data.code == 200) {
        wx.hideLoading();
        shu = res.data.data
        if (that.data.page != 1) {
          if (shu.length != 10) {
            that.setData({
              gongwu: true
            })
          }
        }
        that.setData({
          ylqxList: shuzong.concat(shu)
        })
      }
    })
  },
  // 产品名称
  cpname(e) {
    this.setData({
      cpname: e.detail.value
    })
  },
  // 通用名
  tyname(e) {
    this.setData({
      tyname: e.detail.value
    })
  },
  // 规格
  gg(e) {
    this.setData({
      gg: e.detail.value
    })
  },
  // 型号
  xh(e) {
    this.setData({
      xh: e.detail.value
    })
  },
  // 生产厂商
  sccs(e) {
    this.setData({
      sccs: e.detail.value
    })
  },
  // 报准文号
  bzwh(e) {
    this.setData({
      bzwh: e.detail.value
    })
  },
  // 批号
  ph(e) {
    this.setData({
      ph: e.detail.value
    })
  },
  // 生产日期
  scdate: function (e) {
    this.setData({
      scdate: e.detail.value
    })
  },
  // 有效期至
  yxqz: function (e) {
    if (new Date(e.detail.value).getTime() < Date.parse(new Date())) {
      wx.showToast({
        title: '有效期不可小于当前时间',
        icon: 'none'
      })
      this.setData({
        yxqz: '请选择有效期截止时间'
      })
    } else {
      this.setData({
        yxqz: e.detail.value
      })
    }
  },
  // 数量
  bindManual(e) {
    this.setData({
      num: e.detail.value
    })
  },
  // 上传
  chuan(e) {
    var aaa = e.currentTarget.dataset.aaa
    var that = this;
    //选取图片
    this.myChooseImage(1).then(res => {
      const tempFilePaths = res.tempFilePaths
      this.myUploadImg(tempFilePaths[0], 'file').then(res => {
        var data = JSON.parse(res.data)
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        var urls = data.httpUrl + data.imgUrl
        // 留照
        if (aaa == 1) {
          that.setData({
            lz: urls,
            wulz: data.imgUrl
          })
        }
        // 检验报告
        if (aaa == 2) {
          that.setData({
            jybg: urls,
            wujybg: data.imgUrl
          })
        }
        // 合格证
        if (aaa == 3) {
          that.setData({
            hgz: urls,
            wuhgz: data.imgUrl
          })
        }
      })
    })
  },
  // 点击放大
  fangdas(e) {
    var img = e.currentTarget.dataset.img
    wx.previewImage({
      urls: [img]
    })
  },
  // 验证
  ti() {
    if (this.data.cpname == '') {
      wx.showToast({
        title: '请输入产品名称',
        icon: 'none'
      })
    } else if (this.data.tyname == '') {
      wx.showToast({
        title: '请输入通用名',
        icon: 'none'
      })
    } else if (this.data.gg == '') {
      wx.showToast({
        title: '请输入规格',
        icon: 'none'
      })
    } else if (this.data.xh == '') {
      wx.showToast({
        title: '请输入型号',
        icon: 'none'
      })
    } else if (this.data.sccs == '') {
      wx.showToast({
        title: '请输入生产厂商',
        icon: 'none'
      })
    } else if (this.data.bzwh == '') {
      wx.showToast({
        title: '请输入报准文号',
        icon: 'none'
      })
    } else if (this.data.ph == '') {
      wx.showToast({
        title: '请输入批号',
        icon: 'none'
      })
    } else if (this.data.scdate == '请选择生产日期') {
      wx.showToast({
        title: '请选择生产日期',
        icon: 'none'
      })
    } else if (this.data.yxqz == '请选择有效期截止时间') {
      wx.showToast({
        title: '请选择有效期截止时间',
        icon: 'none'
      })
    } else if (this.data.num == 0) {
      wx.showToast({
        title: '数量不能为0',
        icon: 'none'
      })
    } else if (this.data.wulz == '') {
      wx.showToast({
        title: '请上传留照',
        icon: 'none'
      })
    } else if (this.data.wujybg == '') {
      wx.showToast({
        title: '请上传检验报告',
        icon: 'none'
      })
    } else if (this.data.wuhgz == '') {
      wx.showToast({
        title: '请上传合格证',
        icon: 'none'
      })
    } else {
      this.que()
    }
  },
  // 添加设备
  que() {
    let that = this
    that.setData({
      buttonClicked: true
    })
    this.myRequest(yuming + 'Yiyao/yiyaoQixieAdd', {
      shopId: wx.getStorageSync('shopId'),
      name: that.data.cpname,
      comname: that.data.tyname,
      guige: that.data.gg,
      xinghao: that.data.xh,
      changshang: that.data.sccs,
      baozhunwenhao: that.data.bzwh,
      pihao: that.data.ph,
      startime: that.data.scdate,
      endtime: that.data.yxqz,
      number: that.data.num,
      liuzhao_img: that.data.wulz,
      jianyan_img: that.data.wujybg,
      hege_img: that.data.wuhgz,
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        that.getylqxList();
        setTimeout(function () {
          that.setData({
            currtab: 0,
            buttonClicked: false,
            cpname: '',
            tyname: '',
            gg: '',
            xh: '',
            sccs: '',
            bzwh: '',
            ph: '',
            scdate: '请选择生产日期',
            yxqz: '请选择有效期截止时间',
            num: 1,
            lz: '',
            wulz: '',
            jybg: '',
            wujybg: '',
            hgz: '',
            wuhgz: '',
          })
        }, 1000)
      } else {
        that.setData({
          buttonClicked: false
        })
      }

    })

  },
  // 删除
  shanchu(e) {
    var id = e.currentTarget.dataset.id
    var that = this
    wx.showModal({
      title: '您确定要删除吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: function (res) {
        if (res.confirm) {
          console.log("确定")
          that.dele(id)
        } else {
          console.log("取消")
        }
      }
    })
  },
  // 删除接口
  dele(id) {
    let that = this
    this.myRequest(yuming + 'Yiyao/yiyaoQixieDel', {
      id: id
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        shuzong = [];
        shu = [];
        that.getylqxList();
      }
    })
  },
  /* 加数 */
  addCount: function (e) {
    console.log("刚刚您点击了加1");
    var num = this.data.num;
    // 总数量-1
    if (num < 99) {
      this.data.num++;
    }
    // 将数值与状态写回
    this.setData({
      num: this.data.num
    });
  },
  /* 减数 */
  delCount: function (e) {
    console.log("刚刚您点击了减1");
    var num = this.data.num;
    // 商品总数量-1
    if (num > 1) {
      this.data.num--;
    }
    // 将数值与状态写回
    this.setData({
      num: this.data.num
    });
  },

  showModal(e) {
    var aaa = e.currentTarget.dataset.aaa
    var img = e.currentTarget.dataset.img
    let index = e.currentTarget.id
    this.setData({
      fdaaa: aaa,
      fdimg: img,
      otherxkz_index: index
    })
    let {
      type
    } = e.currentTarget.dataset, va = '';
    if (type == 's2') {
      va = "s2";
    }
    this.setData({
      [va]: true
    })
  },
  confirmbtn(e) {
    console.log(e)
    console.log('点击了从新上传！')
    var aaa = this.data.fdaaa
    var that = this;
    this.myChooseImage(1).then(res => {
      const tempFilePaths = res.tempFilePaths
      this.myUploadImg(tempFilePaths[0], 'file').then(res => {
        var data = JSON.parse(res.data)
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        var urls = data.httpUrl + data.imgUrl
        // 留照
        if (aaa == 1) {
          that.setData({
            lz: urls,
            wulz: data.imgUrl
          })
        }
        // 检验报告
        if (aaa == 2) {
          that.setData({
            jybg: urls,
            wujybg: data.imgUrl
          })
        }
        // 合格证
        if (aaa == 3) {
          that.setData({
            hgz: urls,
            wuhgz: data.imgUrl
          })
        }
      })
    })
  },
  cancelBtn() {
    console.log('点击了放大！')
    wx.previewImage({
      urls: [this.data.fdimg]
    })
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      shopType: options.shopType,
      title: options.title
    })
    this.getylqxList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})